package com.licode.codekit.utils;

import com.licode.codekit.buildprj.CodeKitConf;
import com.licode.codekit.buildprj.ProjectContext;
import org.junit.Test;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author licode
 */
public class StrUtil {
    public static String getFileName(String filePath) {
        String fileName;
        int pos = filePath.lastIndexOf("\\");
        if (-1 == pos) {
            pos = filePath.lastIndexOf("/");
        }
        if (pos != -1) {
            fileName = filePath.substring(pos + 1);
        } else {
            fileName = filePath;
        }

        pos = fileName.lastIndexOf(".");
        if (pos != -1) {
            fileName = fileName.substring(0, pos);
        }

        return fileName;
    }

    public static String getFileDir(String filePath) {
        int pos = filePath.lastIndexOf("\\");
        if (-1 == pos) {
            pos = filePath.lastIndexOf("/");
        }
        if (pos != -1) {
            return filePath.substring(0, pos);
        } else {
            return filePath;
        }
    }

    public static void calcFilePackage(String currDir, ProjectContext prjCtx) {
        int pos = currDir.indexOf(CodeKitConf.SRC_ROOT_PREFIX);
        if (-1 == pos) {
            prjCtx.setFilePackage("");
        } else {
            String pkgDir = currDir.substring(pos + CodeKitConf.SRC_ROOT_PREFIX.length() + 1);
            pkgDir = FileUtil.trimPath(pkgDir);
            pos = pkgDir.lastIndexOf("/");
            if (pos != -1) {
                pkgDir = pkgDir.substring(0, pos);
            }

            prjCtx.setFilePackage(pkgDir);
        }
    }

    public static String makeFirstLower(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    public static String firstLower(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        String mStr = cvModuleName(str);
        if (mStr.length() == 0) {
            return "";
        }

        return mStr.substring(0, 1).toLowerCase()
                + mStr.substring(1);
    }

    public static String cvModuleName(String name) {
        StringBuilder cvName = new StringBuilder();
        if (name == null) {
            return "";
        }
        int len = name.length();

        for (int i = 0; i < len; ++i) {
            String ch = name.substring(i, i +1);
            if (ch.equals("-") || ch.equals("_")) {
                i += 1;
                if (i < len) {
                    ch = name.substring(i, i +1).toUpperCase();
                    cvName.append(ch);
                }
            } else {
                cvName.append(ch);
            }
        }
        return cvName.toString();
    }

    public static String firstUpper(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        String mStr = cvModuleName(str);
        if (mStr.length() == 0) {
            return "";
        }

        return mStr.substring(0, 1).toUpperCase()
                + mStr.substring(1);
    }

    public static String makeFirstUpper(String str) {
        if (str == null || str.length() == 0) {
            return str;
        }

        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }

    @Test
    public void testDate() {
        String strDate = nowTime();
        System.out.println(strDate);
    }

    public static String nowTime() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String convertCamel(String[] strs, boolean capitalize) {
        String retStr = "";
        for (int i = 0; i < strs.length; ++i) {
            if (0 == i && !capitalize) {
                retStr += firstLower(strs[i]);
            } else {
                retStr += firstUpper(strs[i]);
            }
        }
        return retStr;
    }

    public static String getUrlPath(String strs) {
        StringBuilder retStr = new StringBuilder();
        for (int i = 0; i < strs.length(); ++i) {
            if (i > 0 && strs.charAt(i) >= 'A' && strs.charAt(i) <= 'Z') {
                retStr.append("-").append("_").append(strs.substring(i, i + 1).toLowerCase());
            } else {
                retStr.append(strs.charAt(i));
            }
        }
        return retStr.toString();
    }
}
